﻿#include "cimagedownloader.h"
#include "HeaderFile.h"
#include <QLabel>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPixmap>
#include <QDebug>
#include "CPdfReport.h"

CImageDownloader::CImageDownloader(QLabel* label, const QString& imageUrl, QObject* parent)
: QObject(parent), m_imageLabel(label), m_networkManager(new QNetworkAccessManager(this))
{
    //qDebug() << "imageUrl:" << imageUrl << endl;
    QNetworkRequest request;
    request.setUrl(QUrl(imageUrl));

    QNetworkReply* reply = m_networkManager->get(request);
    connect(reply, &QNetworkReply::finished, this, &CImageDownloader::slotOnImageDownloaded);
}


CImageDownloader::CImageDownloader(const QString& imageUrl, QObject* parent /*= nullptr*/)
	: m_sImageUrl(imageUrl), QObject(parent), m_networkManager(new QNetworkAccessManager(this))
{
	
	
}

CImageDownloader::~CImageDownloader()
{
}

//获取最新下载的Pixmap
void CImageDownloader::getNewDlPixmap(QPixmap &NewPixmap)
{
	QNetworkRequest request;
	request.setUrl(QUrl(m_sImageUrl));

	QEventLoop loop;// 使用事件循环等待请求完成
	QTimer timer;  // 使用事件循环等待请求完成
	timer.setSingleShot(true);
	QNetworkReply* reply = m_networkManager->get(request);
	QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
	QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
	timer.start(5000); // 5秒超时
	loop.exec();

	//读取响应
	if (reply->isFinished())
	{
		if (reply->error() == QNetworkReply::NoError) {
			QPixmap pixmap;
			pixmap.loadFromData(reply->readAll());
			NewPixmap = pixmap;
		}
		else
		{

		}
		
	}

	reply->deleteLater();
}
//获取最新下载的Pixmap
void CImageDownloader::getNewDlPixmap(QPixmap &NewPixmap, QVector<CPdfAiPicPolygonArea> vecPdfAiPicPolygonArea, double dImageWid, double dImageHig)
{
	QNetworkRequest request;
	request.setUrl(QUrl(m_sImageUrl));

	QEventLoop loop;// 使用事件循环等待请求完成
	QTimer timer;  // 使用事件循环等待请求完成
	timer.setSingleShot(true);
	QNetworkReply* reply = m_networkManager->get(request);
	QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
	QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
	timer.start(5000); // 5秒超时
	loop.exec();

	//读取响应
	if (reply->isFinished())
	{
		if (reply->error() == QNetworkReply::NoError) {
			QPixmap pixmap;
			pixmap.loadFromData(reply->readAll());

			//画区域处理
			QPixmap scaledPixmap = pixmap.scaled(dImageWid, dImageHig, Qt::KeepAspectRatio, Qt::SmoothTransformation);
			QPainter painter(&scaledPixmap);
			for (int i = 0; i < vecPdfAiPicPolygonArea.size(); i++)
			{
				painter.setPen(QPen(Qt::green, 3));
				painter.drawRect(vecPdfAiPicPolygonArea[i].pPoint.x(), vecPdfAiPicPolygonArea[i].pPoint.y(), vecPdfAiPicPolygonArea[i].dWidth, vecPdfAiPicPolygonArea[i].dHeight);
			}

			NewPixmap = scaledPixmap;
			//NewPixmap = pixmap;
		}
		else 
		{
			
		}
		
	}
	reply->deleteLater();
}



void CImageDownloader::slotOnImageDownloaded()
{
	if (m_imageLabel == nullptr)
	{
		return;
	}
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    if (reply)
	{
        if (reply->error() == QNetworkReply::NoError)
		{
			QPixmap pixmap;
			//pixmap = pixmap.scaled(m_imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            pixmap.loadFromData(reply->readAll());
			m_imageLabel->setPixmap(pixmap);
			m_imageLabel->setScaledContents(true); // 让图片自适应 QLabel 大小
			m_imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // 忽略控件大小策略，以便 QLabel 可以根据需要调整大小
        }
		else
		{
 
        }
        reply->deleteLater();
    }
}
